"""
@author: wtf
@file: models.py
@time: 2024/5/8 16:22
"""
from exts import db
from datetime import datetime
class UserModel(db.Model):
    __tablename__ = "user"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    username = db.Column(db.String(100), nullable=False)
    password = db.Column(db.String(100), nullable=False)
    email = db.Column(db.String(100), nullable=False, unique=True)
    photo = db.Column(db.String(100), default="photos/test.jpg")
    join_time = db.Column(db.DateTime, default=datetime.now)  # 默认值为当前存储时间

    #  author = db.relationship(UserModel, backref="questions")
    # backref="photos

class EmailCaptchaModel(db.Model):
    __tablename__ = "email_captcha"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    email = db.Column(db.String(100), nullable=False, unique=True)
    captcha = db.Column(db.String(100), nullable=False)

class PublicQuestionModel(db.Model):
    __tablename__ = "public_question"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    question = db.Column(db.String(100), nullable=True)
    content = db.Column(db.Text, nullable=False)
    join_time = db.Column(db.DateTime, default=datetime.now)  # 默认值为当前存储时间
    # 设置外键
    author_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    # 搭建联系
    author = db.relationship(UserModel, backref="questions")


class PublicAnswerModel(db.Model):
    __tablename__ = "public_answer"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    content = db.Column(db.Text, nullable=False)
    join_time = db.Column(db.DateTime, default=datetime.now)  # 默认值为当前存储时间
    # 设置外键
    question_id = db.Column(db.Integer, db.ForeignKey('public_question.id'))
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))

    # 搭建联系
    user = db.relationship(UserModel, backref="answers")
    question = db.relationship(PublicQuestionModel, backref=db.backref("answers", order_by=join_time.desc()))

    # 存放相册的模型
class PhotosModel(db.Model):
    __tablename__ = "photos"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    hash = db.Column(db.String(100), nullable=True)
    photo_name = db.Column(db.String(100))
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    join_time = db.Column(db.DateTime, default=datetime.now)  # 默认值为当前存储时间
    user = db.relationship(UserModel, backref="photos")
